package org.oslims.server.dao.hbm;

import java.util.List;

import org.hibernate.Query;
import org.oslims.domain.Operator;
import org.oslims.server.dao.OperatorDao;

public class HbmOperatorDao extends HbmGenericDao<Operator, Long> implements OperatorDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<Operator> findAllWithDepartments() {
		Query q = this.getSession().createQuery("select distinct o from Operator o left join fetch o.departments order by o.firstName");
		return q.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Operator> findAllWithDepartments(int start, int length, String sortBy, Boolean desc) {
		String sortDirection = "asc";

		if (desc)
			sortDirection = "desc";

		Query q = this.getSession().createQuery("select distinct o from Operator o left join fetch o.departments order by o." + sortBy + " " + sortDirection);
		q.setFirstResult(start);
		q.setMaxResults(length);

		return q.list();
	}
}
